'''
Languages supported by web browsers.
'''

from . import _base, _lang

NAME = "Web Browsers"

BROWSER_REG_KEY = r'Software\Clients\StartMenuInternet'

HTML_TEMPLATE = {
    _lang.Html: 
        None,
    _lang.JavaScript: 
        """
        <html>
            <head>
                <title>MEHF</title>
                <script type='text/javascript' src='%s'></script>
            </head>
            <body></body>
        </html>
        """
    }

class WebBrowserEnvironment(_base.Environment):
    
    has_arguments = False
    
    def __init__(self, language, host, path, version, template, cmdline):
        self.language = language
        self.host = host
        self.path = path
        self.version = version
        self.template = template
        self.cmdline = cmdline

    def execute(self, filename):
        if self.template:
            html_filename = filename + '.html'
            self.host.file_write(html_filename, 'wt', self.template % self.host.path_basename(filename))
        else:
            html_filename = filename
        if self.cmdline:
            self.host.call_gui(self.cmdline % html_filename)
        else:
            self.host.call_gui(['"%s"' % self.path, html_filename])

def configure_from_registry(host, environment_list, key, browser):
    try:
        name = host.winreg_query_value(key, r'%s\%s' % (BROWSER_REG_KEY, browser), None)
        
        # Get the executable name from the StartMenuInternet key
        exe = host.winreg_query_value(key, r'%s\%s\shell\open\command' % (BROWSER_REG_KEY, browser), None)
        exe = exe.strip('"')
        
        # Try finding the command line under HKCR.
        cmdline = None
        try:
            classname = host.winreg_query_value(key, r'%s\%s\Capabilities\FileAssociations' % (BROWSER_REG_KEY, browser), '.html')
            cmdline = host.winreg_query_value(host.HKCR, r'%s\shell\open\command' % classname, None)
            if cmdline.find('%1') != -1:
                cmdline = cmdline.replace('%1', '%s')
            else:
                cmdline = cmdline + ' %s'
        except:
            pass
        
        # TODO: Check why Chrome isn't nicely listed under HKLM and make get_file_version work correctly for it
        
        version = host.get_file_version(exe)
        
        for language in [ _lang.Html, _lang.JavaScript ]:
            environment_list[language.name][name].append(WebBrowserEnvironment(language, host, exe, version, HTML_TEMPLATE[language], cmdline))

    except:
        pass

def configure(host_list, environment_list):
    for host in host_list:
        if not hasattr(host, 'call_gui'):
            continue
        if 'nt' in host.os_name_list:
            for key in [ host.HKLM, host.HKCU ]:
                try:
                    for browser in host.winreg_enum_keys(host.HKLM, BROWSER_REG_KEY):
                        configure_from_registry(host, environment_list, key, browser)
                except:
                    pass
        else:
            # TODO: Implement on Linux
            pass
